package com.earnings.okhttputils.utils.ui.activity.common;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.earnings.R;
import com.earnings.okhttputils.utils.bean.ImageData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.utils.MultiTouchViewPager;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;

import java.util.List;

import me.relex.photodraweeview.PhotoDraweeView;

public class ImagePageActivity extends GodLeftHandBaseActivity implements GodOnClickListener {


    public  List<ImageData> images;
    public int pageNum=0;
    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, ImagePageActivity.class));
    }


    @Override
    protected void init() {
        MultiTouchViewPager viewPager = (MultiTouchViewPager) findViewById(R.id.view_pager);
        setHideTopView();
        images= (List<ImageData>)bundleData.getSerializable( "images" );
        viewPager.setAdapter(new DraweePagerAdapter());
        setText( R.id.name ,images.get( pageNum ).getName());
        setText( R.id.info ,images.get( pageNum ).getInfo());
        setText( R.id.page ,(pageNum+1)+"/"+images.size());
        setOnClickListener( this,R.id.backs );
        if(bundleData.containsKey( "pageNum" )){
            pageNum=bundleData.getInt( "pageNum" );
            viewPager.setCurrentItem( pageNum );
        }
        viewPager.setOnPageChangeListener( new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                setText( R.id.name ,images.get( position ).getName());
                setText( R.id.info ,images.get( position ).getInfo());
                setText( R.id.page ,(position+1)+"/"+images.size());
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        } );
    }

    @Override
    protected int onCreateLayoutId() {
        return  R.layout.activity_viewpager;
    }

    @Override
    public void OnClickListener(View view) {
        if(view.getId()==R.id.backs)finish();
    }

    public class DraweePagerAdapter extends PagerAdapter {

      /*  private int[] mDrawables = new int[] {
                R.mipmap.testcode, R.mipmap.add_icon, R.mipmap.testcode
        };
*/
        @Override public int getCount() {
            return images.size();
        }

        @Override public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }


        @Override public Object instantiateItem(ViewGroup viewGroup, int position) {
            final PhotoDraweeView photoDraweeView = new PhotoDraweeView(viewGroup.getContext());
            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
          //  controller.setUri();

if(bundleData.containsKey( "file" )){

    controller.setUri( Uri.fromFile(images.get( position  ).getImgfile()));}
            else  controller.setUri( Uri.parse(images.get( position ).getImgurl()) );
            controller.setOldController(photoDraweeView.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null) {
                        return;
                    }
                    photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });

            photoDraweeView.setController(controller.build());

            try {
                viewGroup.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return photoDraweeView;
        }

    }
}
